<?
include("session.php"); 
include("database.php");

$action = $_GET['action'];
$admin_agent_ids = array("hncsgj01");

if($action == "get_agent_score") {
	$date_start = date("Y-m-d ")." 00:00:00";
	$date_end = date("Y-m-d ")." 23:59:59";
	$task_id = $_GET['task_id'];
	$sql = "SELECT COUNT(0) as cnt,a.agent_id,agent_name FROM hngj_callinfo a LEFT JOIN hngj_agent b ON a.agent_id=b.agent_id 
	WHERE task_id=$task_id AND call_result=1 AND a.agent_id<>'' and call_time between '$date_start' and '$date_end' GROUP BY a.agent_id  ORDER BY cnt DESC  LIMIT 0,3";
	$arr = array();
	$db->query($sql);
	//http_log($sql);
	while($db->next_record()) {
		$arr[] = $db->f("cnt").",".$db->f("agent_name").",".$db->f("agent_id");
	}
	echo join("$", $arr);

} else if($action == "get_the_latest_task") {
	$db->query("select id, task_name from hngj_task order by last_call_time desc limit 0,1");
	$db->next_record();
	echo $db->f("id").",".$db->f("task_name");

}  else if($action == "list_agent_score") {
	$date_start = date("Y-m-d ")." 00:00:00";
	$date_end = date("Y-m-d ")." 23:59:59";
	$task_id = $_GET['task_id'];
	$sql = "SELECT agent_name,a.agent_id,
	sum(CASE call_result WHEN 1 THEN 1 ELSE 0 END) as success_cnt, 
	sum(CASE call_result WHEN 0 THEN 1 ELSE 0 END) as uncall_cnt, 
	COUNT(0) as call_cnt 
	FROM hngj_callinfo a LEFT JOIN hngj_agent b ON a.agent_id=b.agent_id 
	WHERE task_id=$task_id AND a.agent_id<>'' and call_time between '$date_start' and '$date_end'  
	GROUP BY a.agent_id  ORDER BY success_cnt DESC";
	//http_log($sql);
	echo to_grid_json($db, $sql, $dic_data, array("agent_id"=>"blue_color_mapper","agent_name"=>"blue_color_mapper","success_cnt"=>"red_color_mapper"), false);

}  else if($action == "get_task_agent_report") {
	$condition = get_search_condition();
	$sql = "SELECT agent_name, a.agent_id,
	COUNT(0) as call_cnt,
	sum(CASE call_result WHEN 1 THEN 1 ELSE 0 END) as cnt_1, 
	'' as success_rate, 
	sum(CASE call_result WHEN 2 THEN 1 ELSE 0 END) as cnt_2, 
	sum(CASE call_result WHEN 3 THEN 1 ELSE 0 END) as cnt_3,
	sum(CASE call_result WHEN 4 THEN 1 ELSE 0 END) as cnt_4,
	sum(CASE call_result WHEN 5 THEN 1 ELSE 0 END) as cnt_5,
	sum(CASE call_result WHEN 6 THEN 1 ELSE 0 END) as cnt_6,
	sum(CASE call_result WHEN 7 THEN 1 ELSE 0 END) as cnt_7,
	sum(CASE call_result WHEN 8 THEN 1 ELSE 0 END) as cnt_8,
	sum(CASE call_result WHEN 9 THEN 1 ELSE 0 END) as cnt_9,
	sum(CASE call_result WHEN 10 THEN 1 ELSE 0 END) as cnt_10,
	sum(CASE call_result WHEN 11 THEN 1 ELSE 0 END) as cnt_11,
	sum(CASE call_result WHEN 12 THEN 1 ELSE 0 END) as cnt_12  
	from hngj_callinfo a LEFT JOIN hngj_agent b ON a.agent_id=b.agent_id 
	WHERE $condition GROUP BY a.agent_id  ORDER BY cnt_1 DESC";
	//echo $sql;
	echo to_grid_json($db, $sql, $dic_data, array("agent_id"=>"blue_color_mapper","agent_name"=>"blue_color_mapper","success_cnt"=>"red_color_mapper",'success_rate'=>'success_rate_mapper'), false);
}
else if($action == "result_chart_report") { // 结果分类
	$condition = get_search_condition();
	$sql = "SELECT count(0) as call_cnt,
	sum(CASE call_result WHEN 1 THEN 1 ELSE 0 END) as cnt_1, 
	sum(CASE call_result WHEN 2 THEN 1 ELSE 0 END) as cnt_2, 
	sum(CASE call_result WHEN 3 THEN 1 ELSE 0 END) as cnt_3,
	sum(CASE call_result WHEN 4 THEN 1 ELSE 0 END) as cnt_4,
	sum(CASE call_result WHEN 5 THEN 1 ELSE 0 END) as cnt_5,
	sum(CASE call_result WHEN 6 THEN 1 ELSE 0 END) as cnt_6,
	sum(CASE call_result WHEN 7 THEN 1 ELSE 0 END) as cnt_7,
	sum(CASE call_result WHEN 8 THEN 1 ELSE 0 END) as cnt_8,
	sum(CASE call_result WHEN 9 THEN 1 ELSE 0 END) as cnt_9,
	sum(CASE call_result WHEN 10 THEN 1 ELSE 0 END) as cnt_10,
	sum(CASE call_result WHEN 11 THEN 1 ELSE 0 END) as cnt_11,
	sum(CASE call_result WHEN 12 THEN 1 ELSE 0 END) as cnt_12 
	from hngj_callinfo a LEFT JOIN hngj_agent b ON a.agent_id=b.agent_id 
	WHERE $condition";
	//echo $sql;
	$db->query($sql);
	$db->moveNext();
	$data[] = "{'label':'营销成功','value':'".$db->f("cnt_1")."'}";
	$data[] = "{'label':'营销失败','value':'".$db->f("cnt_2")."'}";
	$data[] = "{'label':'预约回拔','value':'".$db->f("cnt_3")."'}";
	$data[] = "{'label':'集团彩铃','value':'".$db->f("cnt_4")."'}";
	$data[] = "{'label':'忙音','value':'".$db->f("cnt_5")."'}";
	$data[] = "{'label':'空号','value':'".$db->f("cnt_6")."'}";
	$data[] = "{'label':'停机','value':'".$db->f("cnt_7")."'}";
	$data[] = "{'label':'无人接听','value':'".$db->f("cnt_8")."'}";
	$data[] = "{'label':'直接挂机','value':'".$db->f("cnt_9")."'}";
	$data[] = "{'label':'非负责人','value':'".$db->f("cnt_10")."'}";
	$data[] = "{'label':'沟通障碍','value':'".$db->f("cnt_11")."'}";
	$data[] = "{'label':'无效数据','value':'".$db->f("cnt_12")."'}";
	$total = $db->f("call_cnt");
	
	print_json("【结果类型统计-外呼总数($total)次】", "A", "B", $data);	
}
else if($action == "day_call_report") {
	$condition = get_search_condition();
	$sql = "select count(0) as cnt, DAY(call_time), DATE(call_time) as ymd from hngj_callinfo a LEFT JOIN hngj_agent b ON a.agent_id=b.agent_id 
	where $condition group by DAY(call_time) order by ymd limit 0,10";
	$db->query($sql);
	$total = 0;
	$data = array();
	while($db->nextRecord()) {
		$data[] = "{'label':'".$db->f("ymd")."','value':'".$db->f("cnt")."', 'link':'j-show_time_report_by_date-".$db->f("ymd")."'}";
		$total += $db->f("cnt");
	}
	print_json("【日期分段统计-外呼总计($total)次】", "日期", "外呼量", $data);
}

else if($action == "time_call_report") {
	$dt = $_GET['dt'];
	$condition = "call_result>0 and a.agent_id<>''". get_select_condition();
	$sql = "select count(0) as cnt, HOUR(call_time) as h from hngj_callinfo a LEFT JOIN hngj_agent b ON a.agent_id=b.agent_id 
	where $condition and call_time between '$dt 00:00:00' and '$dt 23:59:59' group by  HOUR(call_time)";
	$db->query($sql);
	$total = 0;
	$data = array();
	while($db->nextRecord()) {
		$data[] = "{'label':'".$db->f("h")."点','value':'".$db->f("cnt")."'}";
		$total += $db->f("cnt");
	}
	print_json("【时间分段统计-外呼总计($total)次】", "时间段-({$dt})", "外呼量", $data);
}


function success_rate_mapper($v, $arr) {
	if($arr['cnt_1'] == 0) {
		return 0;
	}
	$n = $arr['cnt_1'] / $arr['call_cnt'];
	if($n > 0) {
		return "<font color=red>".substr(strval($n*100),0,4) . "%</font>";
	}
}	


function get_search_condition() {
	$date_range = split("_", $_GET['date_range']);
	$date_start = $date_range[0];
	$date_end = $date_range[1];
	$condition = " a.agent_id<>'' 
	and call_time between '$date_start' and '$date_end'  
	and call_result>0";
	return $condition . get_select_condition();
}

function get_select_condition() {
	$condition = "";
	if( ! empty($_GET['task_id']) ) {
		$condition .= " and a.task_id=".$_GET['task_id'];
	}
	if( ! empty($_GET['agent_group']) ) {
		$condition .= " and b.agent_group='".$_GET['agent_group']."'";
	}
	return $condition;
}


function print_json($title, $xname, $yname, $data) {
	echo "{";
	echo "'chart': {";
	echo "'caption' : '$title',";
	echo "'xAxisName' : '$xname',";
	echo "'yAxisName' : '$yname',";
	echo "'formatNumberScale' : '0',";
	if($xname == "CHANNEL")  {
		// for Bar2D (通道统计明细)
		//---------------------------------
		echo "'alternateVGridColor' : 'AFD8F8',";
		echo "'toolTipBorderColor' : '114B78',";
		echo "'baseFontColor' : '114B78',";
		echo "'toolTipBgColor' : 'E7EFF6',";
		echo "'plotBorderDashed' : '1',";
		echo "'plotBorderDashLen' : '2',";
		echo "'useRoundEdges' : '1',";   
		echo "'plotBorderDashGap' : '2',"; 
		echo "'showBorder' : '0',";   
		echo "'bgColor' : 'FFFFFF,FFFFFF',";   
		//---------------------------------
	}
	echo "'baseFontSize' : '12'},";        
	echo "'data' : [";               
	echo join(", ", $data);
	echo "]";
	echo "}";
}

?>